#ifndef _CVIEWPORT_H
#define _CVIEWPORT_H

#include "CVector2.h"
#include "CVector3.h"

inline bool IsInside( const CVector2f& p, const CVector2f& p1, const CVector2f& p2, const CVector2f& p3, const CVector2f& p4 )
{
	CVector2f v1 = p1 - p;
	CVector2f v2 = p2 - p;
	CVector2f v3 = p3 - p;
	CVector2f v4 = p4 - p;

	if( v1.y*v2.x - v1.x*v2.y > 0 )
	{
		if( v2.y*v3.x - v2.x*v3.y > 0 )
			if( v3.y*v4.x - v3.x*v4.y > 0 )
				if( v4.y*v1.x - v4.x*v1.y > 0 )
					return true;
	}
	else
	{
		if( v2.y*v3.x - v2.x*v3.y < 0 )
			if( v3.y*v4.x - v3.x*v4.y < 0 )
				if( v4.y*v1.x - v4.x*v1.y < 0 )
					return true;
	}

	return false;
}

class CViewPort
{
	struct DeltaPos
	{
		int16			x;
		int16			z;
		short			Dist;

		bool operator < ( const DeltaPos& arg ) const
		{
			return Dist < arg.Dist;
		}
	};

	CVector3f			m_CameraOffset;
	float				m_FovY;
	float				m_Aspect;

	int					m_UseNum;
	vector<DeltaPos>	m_ViewPortDem;

	//
	int16				m_xMin;
	int16				m_zMin;
	int16				m_xMax;
	int16				m_zMax;


public:
	CViewPort(void);
	~CViewPort(void);

	int  Init( CVector3f& Offset, float FovY, float Aspect, float GridSpace );
	int				GridShowNum()		{ return m_UseNum; }
	const DeltaPos&	GetGrid( UINT Num )	{ return m_ViewPortDem[Num]; }

	int16		xMin()					{ return m_xMin; }
	int16		zMin()					{ return m_zMin; }
	int16		xMax()					{ return m_xMax; }
	int16		zMax()					{ return m_zMax; }

};


#endif
